﻿Imports System.Web
Imports System.Text.RegularExpressions
Public Class IWeb_Form
    Private Const Version As Integer = 10000
    'Private IWeb As IWeb.WebCoreServiceClient

    Private _factory As ProxyFactory


    Public ReadOnly Property Factory() As ProxyFactory
        Get

            Return New ProxyFactory(Me.txtWebsite.Text)


        End Get
    End Property
#Region "Properties"

    Public ReadOnly Property Password() As String
        Get
            Dim strPassword As String = ""
            If Me.chkEncrypted.Checked Then
                Dim objIWebEncryption As New IWebEncryption
                strPassword = objIWebEncryption.Encryption(Me.txtPassword.Text, Me.txtKey.Text)
                strPassword = HttpUtility.UrlEncode(strPassword)
            Else
                strPassword = Me.txtPassword.Text
            End If
            Return strPassword
        End Get
    End Property
#End Region

    'Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
    '    Dim strResponse As String = ""

    '    Try

    '        Using IWeb As IWeb.WebCoreServiceClient = Me.Factory.GetProxy(Of IWeb.WebCoreServiceClient)(CoreEndPoints.CoreService)

    '            strResponse = IWeb.EncryptData _
    '            (IWebAuthentication.AttachCredentials(), TextBox1.Text, Me.txtKey.Text)

    '        End Using



    '        Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)

    '    Catch ex As Exception
    '        TextBox2.Text = ex.Message
    '    End Try
    'End Sub

    'Private Sub btnUnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnEncrypt.Click
    '    Dim strResponse As String = ""


    '    Try
    '        Using IWeb As IWeb.WebCoreServiceClient = Me.Factory.GetProxy(Of IWeb.WebCoreServiceClient)(CoreEndPoints.CoreService)


    '            strResponse = IWeb.UnEncryptData _
    '                (IWebAuthentication.AttachCredentials(), HttpUtility.UrlEncode(TextBox1.Text), Me.txtKey.Text)

    '        End Using

    '        Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)
    '    Catch ex As Exception
    '        TextBox2.Text = ex.Message
    '    End Try
    'End Sub

    Private Sub btnGetVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetVersion.Click
        Dim IWebVersion_Info As IWebVersion_Info
        Try
            IWebVersion_Info = IWebVersion.GetVersion()
            Me.lblGetVersion.Text = "Version: " & IWebVersion_Info.VersionName & " (" & IWebVersion_Info.VersionNumber.ToString() & ")"
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnPortals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPortals.Click
        Dim i As Integer = 0
        Dim arrPortals As Array
        Dim colPortals As String = ""
        Dim strPortalNumber As String
        Dim strPortalName As String
        Dim strPortalEntry As String
        Try
            Using IWeb As IWeb.WebCoreServiceClient = Me.Factory.GetProxy(Of IWeb.WebCoreServiceClient)(CoreEndPoints.CoreService)


                arrPortals = IWeb.PortalsOnServer(IWebAuthentication.AttachCredentials()).Split(",")


            End Using
            For i = 0 To arrPortals.Length - 1 Step 2
                strPortalNumber = arrPortals(i).ToString()
                strPortalName = arrPortals(i + 1).ToString()
                strPortalEntry = strPortalNumber & " [" & strPortalName & "]"
                colPortals = colPortals & strPortalEntry & vbCrLf
            Next
            lblPortals.Text = colPortals.ToString
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnCheckVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckVersion.Click

        If IWebVersion.CheckVersion(Version) Then
            MessageBox.Show("The IWeb version on the server is correct")
        Else
            MessageBox.Show("The IWeb version on the server is too low")
        End If
    End Sub

    Private Sub btnGetUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetUser.Click
        Dim objUserInfo As IWeb.UserInfoDataContract

        Try
            Using IWeb As IWeb.WebUserManagerClient = Me.Factory.GetProxy(Of IWeb.WebUserManagerClient)(CoreEndPoints.WebUserManager)


              

                objUserInfo = IWeb.GetUser(IWebAuthentication.AttachCredentials(), txtUser.Text)

                lblShowDisplayName.Text = objUserInfo.DisplayName
                lblShowFirstName.Text = objUserInfo.FirstName
                lblShowLastName.Text = objUserInfo.LastName
                lblShowEmail.Text = objUserInfo.Email
                lblShowPassword.Text = objUserInfo.Password

            End Using

        Catch ex As Exception
            Me.lblShowDisplayName.Text = ex.Message
        End Try

    End Sub

    Private Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click

        Try
           
            Using IWeb As IWeb.WebUserManagerClient = Me.Factory.GetProxy(Of IWeb.WebUserManagerClient)(CoreEndPoints.WebUserManager)

                lblCreateStatus.Text = IWeb.CreateUser _
                    (IWebAuthentication.AttachCredentials(), _
                     txtDisplayName.Text, txtEmail.Text, _
                     txtFirstName.Text, txtLastName.Text, _
                     txtSetPassword.Text, txtSetUserName.Text)

            End Using
        Catch ex As Exception
            lblCreateStatus.Text = ex.Message
        End Try

    End Sub


End Class

